package edu.ruc.cluster.host;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.SftpException;

import edu.ruc.cluster.util.DefaultLogHandler;
import edu.ruc.cluster.util.SFTPChannel;
import edu.ruc.cluster.util.Const;
import edu.ruc.cluster.util.FileProgressMonitor;

/**
 * 2012-12-06
 * This class is used for ftp transfer. It transfer the Const.IP_LIST_FILE to user's home folder(/home/usrname)
 * as Const.HOST_ADD_FILE, which can be fetched as ~/Const.HOST_ADD_FILE
 * @author deke
 */
public class HostsFileGenerator {

	public static final DefaultLogHandler logHandler = DefaultLogHandler.getInstance();
    
	public SFTPChannel getSFTPChannel() {
        return new SFTPChannel();
    }

    /**
     * @param args
     * @throws Exception
     */
    public void ftp(String src, String dst, Map<String, String> info){
    	
    	try{
    		SFTPChannel channel = getSFTPChannel();
    		ChannelSftp chSftp = channel.getChannel(info, 60000);
    		File file = new File(src);
    		long fileSize = file.length();
    		    		
    		/**
             * code segment 1
            OutputStream out = chSftp.put(dst, ChannelSftp.OVERWRITE); // OVERWRITE mode
            byte[] buff = new byte[1024 * 256]; // buffer transfer per time : 256KB
            int read;
            if (out != null) {
                System.out.println("Start to read input stream");
                InputStream is = new FileInputStream(src);
                do {
                    read = is.read(buff, 0, buff.length);
                    if (read > 0) {
                        out.write(buff, 0, read);
                    }
                    out.flush();
                } while (read >= 0);
                System.out.println("input stream read done.");
            }
            **/
            
            chSftp.put(src, dst, new FileProgressMonitor(fileSize), ChannelSftp.OVERWRITE); // code segment 2
            
            // chSftp.put(new FileInputStream(src), dst, ChannelSftp.OVERWRITE); // code segment 3
            
            chSftp.quit();
            channel.closeChannel();
            
    	}catch(JSchException e){
    		logHandler.error(this.getClass().getSimpleName() + e.getMessage(), e);
    	}catch(SftpException e){
    		logHandler.error(this.getClass().getSimpleName() + e.getMessage(), e);
    	}catch(Exception e){
    		logHandler.error(this.getClass().getSimpleName() + e.getMessage(), e);
    	}
    }
    
    public void transform(String host, String username, String passwd){
    	
    	Map<String, String> sftpDetails = new HashMap<String, String>();
        
    	//setup host ip， port, username, password
        sftpDetails.put(Const.SFTP_REQ_HOST, host);
        sftpDetails.put(Const.SFTP_REQ_USERNAME, username);
        sftpDetails.put(Const.SFTP_REQ_PASSWORD, passwd);
        sftpDetails.put(Const.SFTP_REQ_PORT, "22");
        
        String src = Const.IP_LIST_FILE;
        String dst = "/home/" + username + "/" + Const.HOSTS_ADD_FILE; 
        ftp(src, dst, sftpDetails);
        
    }
    /*
    
    // shows how to call the function
    public static void main(String[] args) throws Exception {
    	
        HostsFileGenerator test = new HostsFileGenerator();      
//        String host = "dlnode26";
//        String username = "abc";
//        String passwd = "123456";
        String host = "10.77.110.202";
        String username = "deke";
        String passwd = "111111";
        test.transform(host, username, passwd);
        
    }
    */
}